//
//  GiftListView.swift
//  七哥直播
//
//  Created by seven on 2017/12/26.
//  Copyright © 2017年 seven. All rights reserved.
//

import UIKit

private let kGiftCellID = "kGiftCellID"

protocol GiftListViewDelegate: class {
    func giftListView(giftView: GiftListView, giftModel: GiftModel)
}

class GiftListView: UIView,NibLoadable {

    
    @IBOutlet weak var giftContentView: GiftListView!
    @IBOutlet weak var sendGiftBtn: UIButton!
    
    private var pageCollectionView: LXPageCollectonView!
    private var currentIndexPath: IndexPath?
    private var giftVM: GiftViewModel = GiftViewModel()
    
    weak var delegate: GiftListViewDelegate?
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupUI()
    }

    override func awakeFromNib() {
        super.awakeFromNib()
//        setupUI()
        loadGiftData()
    }
    
}

//MARK:- 送礼物
extension GiftListView{
    
    @IBAction func sendGiftBtnClick(_ sender: UIButton) {
        let package = giftVM.giftlistData[currentIndexPath!.section]
        let giftModel = package.list[currentIndexPath!.item]
        delegate?.giftListView(giftView: self, giftModel: giftModel)
    }
    
}

//MARK:- 加载数据
extension GiftListView{
    private func loadGiftData(){
        giftVM.loadGiftData {
            self.pageCollectionView.reloadData()
        }
    }
}
//MARK:- UI相关
extension GiftListView{
    private func setupUI(){
        setupGiftView()
    }
    
    private func setupGiftView(){
        let style = LXTitleStyle()
        style.isScrollEnable = false
        style.isShowIndicatorLine = true
        style.normalColor = UIColor(R: 255, G: 255, B: 255)
        
        let layout = LXPageCollectionViewLayout()
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        layout.minimumLineSpacing = 5
        layout.minimumInteritemSpacing = 5
        layout.rows = 2
        layout.cols = 4
        
        var pageFrame = giftContentView.bounds
        pageFrame.size.width = kScreenW
        pageCollectionView = LXPageCollectonView(frame: pageFrame, titles: ["热门","高级","豪华","专属"], style: style, isTitleInTop: true, layout: layout)
        giftContentView.addSubview(pageCollectionView)

        pageCollectionView.dataSource = self
        pageCollectionView.delegate = self
        pageCollectionView.register(nib: UINib(nibName: "GiftViewCell", bundle: nil), identifier: kGiftCellID)
    }
}

extension GiftListView: LXPageCollectionViewDataSource,LXPageCollectonViewDelegate{
    func numberOfSections(in pageCollectionView: LXPageCollectonView) -> Int {
        return giftVM.giftlistData.count
    }
    func pageCollectionView(_ collectionView: LXPageCollectonView, numberOfItemsInSection section: Int) -> Int {
        let package = giftVM.giftlistData[section]
        return package.list.count
    }
    func pageCollectionView(_ pageCollectionView: LXPageCollectonView, _ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kGiftCellID, for: indexPath) as! GiftViewCell
        cell.giftModel = giftVM.giftlistData[indexPath.section].list[indexPath.row]
        return cell
    }

    func pageCollectionView(_ collectionView: LXPageCollectonView, didSelectItemAt indexPath: IndexPath) {
        currentIndexPath = indexPath
        sendGiftBtn.isEnabled = true
    }
}

